package handlers

import (
	"context"
	"ourplansystem/server/ent"
	"ourplansystem/server/models"
	"strconv"

	"github.com/gofiber/fiber/v2"
)

// GiftHandler 礼物心愿单处理器
type GiftHandler struct {
	client *ent.Client
}

// NewGiftHandler 创建礼物心愿单处理器
func NewGiftHandler(client *ent.Client) *GiftHandler {
	return &GiftHandler{client: client}
}

// CreateGiftRequest 创建礼物请求
type CreateGiftRequest struct {
	Title       string  `json:"title"`
	Description string  `json:"description"`
	ImageURL    string  `json:"image_url"`
	Price       float64 `json:"price"`
	Status      string  `json:"status"`
	FromUserID  int64   `json:"from_user_id"`
	ToUserID    int64   `json:"to_user_id"`
}

// UpdateGiftRequest 更新礼物请求
type UpdateGiftRequest struct {
	Title       string  `json:"title"`
	Description string  `json:"description"`
	ImageURL    string  `json:"image_url"`
	Price       float64 `json:"price"`
	Status      string  `json:"status"`
	FromUserID  int64   `json:"from_user_id"`
}

// GetGifts 获取所有礼物
func (h *GiftHandler) GetGifts(c *fiber.Ctx) error {
	ctx := context.Background()
	gifts, err := h.client.Gift.Query().
		WithFromUser().
		WithToUser().
		All(ctx)
	if err != nil {
		return models.InternalServerError(c, "获取礼物列表失败")
	}

	return models.Success(c, gifts)
}

// GetGift 获取单个礼物
func (h *GiftHandler) GetGift(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的礼物ID")
	}

	gift, err := h.client.Gift.Get(ctx, id)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "礼物不存在")
		}
		return models.InternalServerError(c, "获取礼物失败")
	}

	return models.Success(c, gift)
}

// CreateGift 创建礼物
func (h *GiftHandler) CreateGift(c *fiber.Ctx) error {
	ctx := context.Background()
	var req CreateGiftRequest
	if err := c.BodyParser(&req); err != nil {
		return models.BadRequest(c, "请求参数错误")
	}

	if req.Title == "" {
		return models.BadRequest(c, "标题不能为空")
	}

	if req.ToUserID == 0 {
		return models.BadRequest(c, "接收者ID不能为空")
	}

	builder := h.client.Gift.
		Create().
		SetTitle(req.Title).
		SetToUserID(req.ToUserID)

	if req.Description != "" {
		builder.SetDescription(req.Description)
	}

	if req.ImageURL != "" {
		builder.SetImageURL(req.ImageURL)
	}

	if req.Price > 0 {
		builder.SetPrice(req.Price)
	}

	if req.Status != "" {
		builder.SetStatus(req.Status)
	}

	if req.FromUserID > 0 {
		builder.SetFromUserID(req.FromUserID)
	}

	gift, err := builder.Save(ctx)
	if err != nil {
		return models.InternalServerError(c, "创建礼物失败")
	}

	return models.SuccessWithMessage(c, "创建成功", gift)
}

// UpdateGift 更新礼物
func (h *GiftHandler) UpdateGift(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的礼物ID")
	}

	var req UpdateGiftRequest
	if err := c.BodyParser(&req); err != nil {
		return models.BadRequest(c, "请求参数错误")
	}

	builder := h.client.Gift.UpdateOneID(id)

	if req.Title != "" {
		builder.SetTitle(req.Title)
	}

	if req.Description != "" {
		builder.SetDescription(req.Description)
	}

	if req.ImageURL != "" {
		builder.SetImageURL(req.ImageURL)
	}

	if req.Price > 0 {
		builder.SetPrice(req.Price)
	}

	if req.Status != "" {
		builder.SetStatus(req.Status)
	}

	if req.FromUserID > 0 {
		builder.SetFromUserID(req.FromUserID)
	}

	gift, err := builder.Save(ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "礼物不存在")
		}
		return models.InternalServerError(c, "更新礼物失败")
	}

	return models.SuccessWithMessage(c, "更新成功", gift)
}

// DeleteGift 删除礼物
func (h *GiftHandler) DeleteGift(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的礼物ID")
	}

	err = h.client.Gift.DeleteOneID(id).Exec(ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "礼物不存在")
		}
		return models.InternalServerError(c, "删除礼物失败")
	}

	return models.SuccessWithMessage(c, "删除成功", nil)
}
